package com.zhongjian.technologycal.mall.fragment;


import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.classify.activity.CategoryChildListActivity;
import com.zhongjian.technologycal.mall.activity.main.activity.SearchDataActivity;
import com.zhongjian.technologycal.mall.activity.main.activity.SimpleCaptureActivity;
import com.zhongjian.technologycal.mall.activity.main.activity.SystemMessageActivity;
import com.zhongjian.technologycal.mall.adapter.HomeItemAdapter;
import com.zhongjian.technologycal.mall.adapter.MenuAdapter;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.base.BaseFragment;
import com.zhongjian.technologycal.mall.bean.CategoryProductBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.LogUtils;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.view.GridViewForScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/9/18.
 * 商品分类的实现
 */
public class ShopCategoryFragment extends BaseFragment {
    @BindView(R.id.img_qrcode)
    ImageView img_qrcode;
    @BindView(R.id.lv_menu)
    ListView lv_menu;
    @BindView(R.id.gridview_brand_more)
    GridViewForScrollView gridview_brand_more;
    @BindView(R.id.more_tv_title)
    TextView more_tv_title;
    private int mainSelectPostion = 0;
    @BindView(R.id.category_SmartRefreshLayout)
    RefreshLayout category_SmartRefreshLayout;

    private List<CategoryProductBean.DataBean> menuList = new ArrayList<>();
    private List<CategoryProductBean.DataBean.ProductSubCategoriesBean> moreList = new ArrayList<>();
    private List<Integer> showTitle;

    private MenuAdapter menuAdapter;
    private HomeItemAdapter homeItemAdapter;
    private int currentItem;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_classify;
    }

    @Override
    protected void initView() {
//        StatusBarUtil.setColorNoTranslucent(getActivity(), getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(getActivity(), true);
        StatusBarCompat.setStatusBarColor(getActivity(), getResources().getColor(R.color.border_color), true);
        category_SmartRefreshLayout.setEnableLoadmore(false);
        initViewRequest();
        initListener();
        category_SmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                category_SmartRefreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initViewRequest();
                        category_SmartRefreshLayout.finishRefresh();
                    }
                }, 1000);
            }
        });

    }

    @Override
    public void initPresenter() {

    }

    private void initViewRequest() {
        if (!NetWorkUtils.isNetConnected(getActivity())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getActivity()).getRequest(BaseContacts.URL_CATEGORY_PRODUCT,
                new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        LoadingDialog.showDialogForLoading(getActivity());
                    }

                    @Override
                    public void onCompleted() {
                        Log.d("onCompleted", "请求完成");
                        LoadingDialog.cancelDialogForLoading();
                    }
                    @Override
                    public void onError(Throwable e) {
                        LoadingDialog.cancelDialogForLoading();
                        LogUtils.logd(e.getMessage());
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        LoadingDialog.cancelDialogForLoading();
                        try {
                            String result = responseBody.string();
                            Gson gson = new Gson();
                            CategoryProductBean categoryProductBean = gson.fromJson(result, CategoryProductBean.class);
                            if (categoryProductBean.getError_code() == 0) {
                                try {
                                    menuList = categoryProductBean.getData();
                                    more_tv_title.setText("" + menuList.get(mainSelectPostion).getCategoryname());
                                    fillBrandMainUI();
                                    fillBrandSonUI();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                ToastUitl.showShort(categoryProductBean.getError_message());
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });


    }


    /**
     * 跳转扫一扫页面
     */
    @OnClick(R.id.img_qrcode)
    public void qrCodeOpenAct() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), SimpleCaptureActivity.class);
        startActivity(intent);
    }

    /**
     * 跳转系统消息
     */
    @OnClick(R.id.iv_system_message)
    public void appOnClickOpenSystemMessage() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), SystemMessageActivity.class);
        startActivity(intent);
    }

    /**
     * 搜索
     */
    @OnClick(R.id.et_search)
    public void appOnClickSearch() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), SearchDataActivity.class);
        startActivity(intent);
    }

    /**
     * 监听
     */
    private void initListener() {
        lv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mainSelectPostion = position;
                menuAdapter.setSelectItem(position);
                menuAdapter.notifyDataSetInvalidated();
                fillBrandSonUI();
                homeItemAdapter.notifyDataSetChanged();
                more_tv_title.setText("" + menuList.get(mainSelectPostion).getCategoryname());

            }
        });

        gridview_brand_more.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), CategoryChildListActivity.class);
                intent.putExtra("categoryId", String.valueOf(moreList.get(position).getId()));
                intent.putExtra("categoryName", String.valueOf(moreList.get(position).getCategoryname()));
                startActivity(intent);
            }
        });
    }


    /**
     * 填充子类
     */
    private void fillBrandSonUI() {
        moreList = menuList.get(mainSelectPostion).getProductSubCategories();
        homeItemAdapter = new HomeItemAdapter(getActivity(), moreList);
        gridview_brand_more.setAdapter(homeItemAdapter);
    }

    /**
     * 填充父类
     */
    private void fillBrandMainUI() {
        if (menuAdapter == null) {
            menuAdapter = new MenuAdapter(getActivity(), menuList);
            lv_menu.setAdapter(menuAdapter);
        }

    }

}
